package com.jeason.blogservice.component;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.jeason.blogcommon.utils.RedisUtil;
import com.jeason.blogcommon.utils.RequestHolder;
import com.jeason.blogmpg.vo.FileVO;
import com.jeason.blogservice.constant.SysConf;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import sun.rmi.runtime.Log;

import java.util.List;

/**
 * @Author: jeason
 * @Description:
 * @Date: 2021/1/23 14:18
 */
@Component
@Slf4j
public class PictureFeignClient {
    @Value("${picture-server.host}")
    private String pictureHost;

    @Value("${picture-server.port}")
    private String picturePort;

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 获取文件的信息接口
     *
     * @param fileIds 图片uid
     * @param code    分隔符
     * @return
     */
    public String getPicture(String fileIds, String code){
        String url = "http://" + pictureHost + ":" + picturePort + "/file/getPicture?fileIds=" + fileIds + "&code=" + code;
        String authHeader = RequestHolder.getRequest().getParameter(SysConf.TOKEN);
        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", authHeader);
        HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
        try {
            ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
            return responseEntity.getBody();
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.toString());
        }
        return "";
    }

    /**
     * 通过URL List上传图片
     *
     * @param fileVO
     * @return
     */
    public String uploadPicsByUrl(FileVO fileVO){
        String url = "http://" + pictureHost + ":" + picturePort + "/file/uploadPicsByUrl";
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("fileVO",fileVO);
        HttpHeaders header = new HttpHeaders();
        // 需求需要传参为form-data格式
        header.setContentType(MediaType.MULTIPART_FORM_DATA);
        HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity(map, header);
        JSONObject response = restTemplate.postForObject(url, httpEntity, JSONObject.class);
        assert response != null;
        return response.toString();
    }

    /**
     * 通过管理员uid列表获取存储信息
     * @param adminUidList
     * @return
     */
    public String getStorageByAdminUid(List<String> adminUidList) {
        String url = "http://" + pictureHost + ":" + picturePort + "/storage/getStorageByAdminUid?adminUidList=" + JSONUtil.toJsonStr(adminUidList);
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
        return responseEntity.getBody();
    }

    public String initStorageSize(String adminUid, long maxStorageSize) {
        String url = "http://" + pictureHost + ":" + picturePort + "/storage/initStorageSize";
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("adminUid",adminUid);
        map.add("maxStorageSize",maxStorageSize);
        HttpHeaders header = new HttpHeaders();
        // 需求需要传参为form-data格式
        header.setContentType(MediaType.MULTIPART_FORM_DATA);
        HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity(map, header);
        JSONObject response = restTemplate.postForObject(url, httpEntity, JSONObject.class);
        assert response != null;
        return response.toString();
    }

    public String editStorageSize(String adminUid, long maxStorageSize) {
        String url = "http://" + pictureHost + ":" + picturePort + "/storage/editStorageSize";
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("adminUid",adminUid);
        map.add("maxStorageSize",maxStorageSize);
        HttpHeaders header = new HttpHeaders();
        // 需求需要传参为form-data格式
        header.setContentType(MediaType.MULTIPART_FORM_DATA);
        HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity(map, header);
        JSONObject response = restTemplate.postForObject(url, httpEntity, JSONObject.class);
        assert response != null;
        return response.toString();
    }
}
